from celery import Celery
from dailyfresh import settings
from django.core.mail import send_mail
import os
import django
from django.template import loader, RequestContext
from django.shortcuts import render
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dailyfresh.settings')
django.setup()
app=Celery("celery_tasks.task",broker='redis://192.168.59.137:6379/8')
from goods.models import *
@app.task
def sendemail(email,username,token):
    subject="天天生鲜欢迎信息"
    message=""
    htmlmessage="<h1>欢迎{}成为我们的会员</h1></br><a href='http://127.0.0.1:8000/user/active/{}'>点击激活</a>".format(username,token)
    sender=settings.EMAIL_FROM
    receiver=[email]
    send_mail(subject,message,sender,receiver,html_message=htmlmessage)
@app.task
def generate_static_index():
    types = GoodsType.objects.all()
    advs=IndexPromotionBanner.objects.all().order_by('index')
    shows=IndexGoodsBanner.objects.all().order_by('index')
    for type in types:
        pic=IndexTypeGoodsBanner.objects.filter(type=type,display_type=1).order_by('index')
        word=IndexTypeGoodsBanner.objects.filter(type=type,display_type=0).order_by('index')
        type.pic=pic
        type.word=word
    content={
        'types':types,
        'advs':advs,
        'shows':shows,
    }
    temp = loader.get_template('static_index.html')
    # # 2.定义模板上下文
    # context = RequestContext(request, context)
    # 3.模板渲染
    content = temp.render(content)
    path=os.path.join(settings.BASE_DIR,'static/index.html')
    with open(path,'w') as f:
        f.write(content)

